var mongo = require("mongodb");
var host = "localhost";
var port = mongo.Connection.DEFAULT_PORT;
var server = new mongo.Server(host, port, {auto_reconnect: true});
var db = new mongo.Db('test', server, {safe: true});

db.open(function(err, db){
	if(err)
		throw err;
	else{
		db.collection('users', function(err, collection){
			collection.insert({username: 'cgg', firstname: '楚'}, function(err, docs){
				if(err)
					throw err;
				else{
					console.log(docs);
					db.close(false);
					// 改为true，报错
					// db.close(true);
				}
			});
		});
	}
});


db.once('close', function(err, db){
	if(err)
		throw err;
	else{
		db.open(function(err, db){
			if(err)
				throw err;
			else{
				db.collection('users', function(err, collection){
					collection.insert({username: 'cgg2', firstname: '楚2'}, function(err, docs){
						if(err)
							throw err;
						else{
							console.log(docs);
							db.close(true);
						}
					});
				});
			}
		});
	}
});


